
$(function() {

	$("input[id^='name_s']:visible").autocomplete(Test.potential_name);	
	
	Tip.emp('负责人');
});

var PotentialMy = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;			
				body.fullname = form.fullname.value;
				
				body.category = form.category.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;
				body.content = form.content.value;
				body.emp = form.emp.value;

				// body.contact = $.toJSON(aa);
				
				$(this).dialog("close");

				$.post('customer.php?do=PotentialMy.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
		
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status, type) {
		$.cookie('id_', id);
		
		// alert(type);
		// a = $.evalJSON(s);
		// alert(a);
		
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'customer.php?do=PotentialMy.edit&id=' + id;	
		var c2 = 'customer.php?do=PotentialMy.view&id=' + id;

		var c3 = 'customer.php?do=PotentialMy.customer&id=' + id;
		var c4 = 'customer.php?do=PotentialMy.supplier&id=' + id;

		var a1 = ['编辑信息', 'url', 'PotentialMy.dialog_edit_', '650x450'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
		
		var a3 = ['添加为客户', 'url', 'PotentialMy.dialog_customer', '650x450'];
		var a4 = ['添加为供应商', 'url', 'PotentialMy.dialog_supplier', '650x450'];
		
		if (type == '1') {
			var s = c1 + ", " + c2 + ", " + c4;

			return popup(e, [a1, a2, a4], s);
		} else if (type == '2') {
			var s = c1 + ", " + c2 + ", " + c3;

			return popup(e, [a1, a2, a3], s);	
		} else if (type == '3') {
			var s = c1 + ", " + c2;

			return popup(e, [a1, a2], s);
		}
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4;

		return popup(e, [a1, a2, a3, a4], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=PotentialMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						if (!ContactEdit.validate_edit()) {
							return false;
						}
			
						var id = $("input[name='id']");
						var linkman = $("input[name='linkman']");
						var job = $("input[name='job']");
						var tel = $("input[name='tel']");
						var fax = $("input[name='fax']");
						var mobile = $("input[name='mobile']");
						var email = $("input[name='email']");
						var address = $("input[name='address']");
						
						var qq = $("input[name='qq']");
						var skype = $("input[name='skype']");
						var content = $("input[name='content']");
			
						var aa = new Array();
						for (var i = 0; i < z.length; i++) {
							var contact = {};
							var b = $("#form" + z[i] + " :input[name='id']");
							if (b != null) {
								contact.id = $("#form" + z[i] + " :input[name='id']").val();
							}
			
							contact.linkman = $(linkman[i]).val();
							contact.job = $(job[i]).val();
							contact.tel = $(tel[i]).val();
							contact.fax = $(fax[i]).val();
							contact.mobile = $(mobile[i]).val();
							contact.email = $(email[i]).val();
							contact.address = $(address[i]).val();
							
							contact.qq = $(qq[i]).val();
							contact.skype = $(skype[i]).val();
							contact.content = $(content[i]).val();
							
							aa.push(contact);
						}
			
						var form = $("#form_edit")[0];
			
						var star = $("input[name='star']:checked");
			
						var body = { };
						body.id = form.info_id.value;
						body.name = form.name.value;
						body.fullname = form.fullname.value;
						body.star = star.val();
			
						body.category = form.category.value;
									
						body.address = form.address.value;
						body.url = form.url.value;
						body.jj = form.jj.value;
						body.content = form.content.value;
						body.emp = form.emp.value;
						body.status = form.status.value;
			
						body.contact = $.toJSON(aa);
			
						body.z = $.cookie('contact');
			
						$.cookie('contact', null);
			
						$(this).dialog("close");
						
						var url = 'customer.php?do=PotentialMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_customer: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
								
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();

					aa.push(contact);
				}

				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.info_id.value;
				body.code = form.code.value;
				body.code_t = form.code_t.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;

				body.contact = $.toJSON(aa);
				
				$(this).dialog("close");

				$.post('customer.php?do=PotentialMy.customerPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_supplier: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
		
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");
	
				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}
	
				var form = $("#form_edit")[0];
	
				var body = { };
				body.id = form.info_id.value;
				body.name = form.name.value;			
				body.fullname = form.fullname.value;
				
				body.category = form.category.value;
				body.type = form.type.value;
		
				body.address = form.address.value;
				
				body.pay = form.pay.value;
				body.currency = form.currency.value;
				body.tax = form.tax.value;
				body.url = form.url.value;
				body.share = form.share.value;
				
				body.jj = form.jj.value;
				body.content = form.content.value;
	
				body.contact = $.toJSON(aa);
				body.emp = form.emp.value;
				
				$(this).dialog("close");
	
				$.post('customer.php?do=PotentialMy.supplierPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	}
};
